<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="10"><div class="grid-content bg-purple">
        <div class="developer left">
          <div id="box">
            <ul>
              <li v-for="(v, index) in districtList" :key="index">
                <div class="product-content1">
                  <image-preview :src="v.image" alt="Dynamic Image" class="fixed-image-size"/>
                  <div class="info">
                    <h2>{{ v.communityName }}</h2>
                    <h4>小区地址：暂无地址</h4>
                    <h4>辖区：{{ v.name }}</h4>
                  </div>
                  <div class="additional-info1">
                    <p @click="handlePopulationClick(v.id)">实有人口：{{ v.populationNum }}</p>
                    <p @click="handleEstateClick(v.id)">实有房屋：{{ v.buildingCount }}</p>
                    <p @click="handleUnitClick(v.id)">实有单位：{{ v.unitNum }}</p>
                    <p @click="handleVehiclesClick(v.id)">实有车辆：{{ v.vehicleNum }}</p>
                    <p @click="handleCacheClick(v.id)">实有力量：{{ v.realEstateNum }}</p>
                    <p @click="handleFacilitiesClick(v.id)">实有设备：{{ v.securityFacilitiesNum }}</p>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div></el-col>
      <el-col :span="10"><div class="grid-content bg-purple">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备类型" prop="facilityType">
        <el-select v-model="queryParams.facilityType" :clearable="true" placeholder="请选择设备类型">
          <el-option
            v-for="item in facility"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <br>
      <el-form-item label="安装日期" prop="max">
        <el-date-picker clearable
          v-model="queryParams.max"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期">
        </el-date-picker>
      </el-form-item>
        至
      <el-form-item prop="min">
        <el-date-picker clearable
                        v-model="queryParams.min"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维护日期" prop="max1">
        <el-date-picker clearable
          v-model="queryParams.max1"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期">
        </el-date-picker>
      </el-form-item>至
        <el-form-item prop="min1">
        <el-date-picker clearable
                        v-model="queryParams.min1"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="max2">
        <el-date-picker clearable
          v-model="queryParams.max2"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始时间">
        </el-date-picker>
      </el-form-item>至
        <el-form-item prop="min2">
        <el-date-picker clearable
                        v-model="queryParams.min2"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['community:facilities:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"

          @click="handleUpdate"
          v-hasPermi="['community:facilities:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"

          @click="handleDelete"
          v-hasPermi="['community:facilities:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['community:facilities:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
        <div id="box">
          <ul class="unit-list">
            <li v-for="(v, index) in facilitiesList" :key="index" class="fixed-image-size1">
              {{v.id}}
              <div class="product-unit">
                <div class="info">
                  <input type="checkbox" v-model="selectedItems" :value="v.id" class="checkbox-item" @change="handleSelectionChange(v.id)" />
                  <h4><dict-tag :options="dict.type.real_security_facilities_type" :value="v.facilityType" /></h4>
                  <h4>设备名称：{{ v.name }}</h4>
                  <h4>小区名称：{{ v.communityName}}</h4>
                  <h4>安装日期：{{ v.installedDate }}</h4>
                  <h4>维护日期：{{ v.maintenanceDate }}</h4>
                  <h4>创建时间：{{ v.createDate }}</h4>
                  <button @click="handleVideoClick(v.id)">进入视频</button>
                </div>
              </div>
            </li>
          </ul>
        </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </div></el-col>
    </el-row>

    <!-- 添加或修改实有安防设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备类型" prop="facilityType">
          <el-select v-model="form.facilityType" placeholder="请选择设备类型">
            <el-option
              v-for="item in facility"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="安装日期" prop="installedDate">
          <div class="block">
            <el-date-picker clearable
              v-model="form.installedDate"
              type="datetime"
              placeholder="选择安装日期时间"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFacilities, getFacilities, delFacilities, addFacilities, updateFacilities } from "@/api/community/facilities";
import { getDistrictSelect } from '@/api/community/district'
import {} from "../../../api/community/district";
import {securityFacilitiesTypeList} from "../../../api/community/facilities";
import { listDistrict } from '@/api/community/district'
import { getVehicles } from '@/api/community/vehicles'
export default {
  name: "Facilities",
  dicts: ['real_security_facilities_type'],
  data() {
    return {
      facility:[],
      districtList:[],
      district:[],
      facilityTypeList:[],
      selectedItems:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      //single: true,
      // 非多个禁用
     // multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实有安防设施表格数据
      facilitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        facilityType: null,
        districtId: null,
        installedDate: null,
        maintenanceDate: null,
        createDate: null,
        max:null,
        min:null,
        max1:null,
        min1:null,
        max2:null,
        min2:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      districtId: ''
    };
  },
  created() {
    this.districtId = localStorage.getItem('districtId')
    var id = this.$route.params.id
    if (id != null && id != undefined && id !== '') {
      localStorage.setItem('districtId', id);
    }
    this.getList();
    this.getSelect();
    this.getfacilitiesSelect();
    this.getDistrictList();
  },
  methods: {
    handleVideoClick(id){
      this.$router.push({
        path: '/community/video/'+id,
        name: 'VideoPlayer',
        query: {
          id: id
        }
      })
    },
    // 设备类型下拉框查询
    getfacilitiesSelect(){
      securityFacilitiesTypeList().then(response => {
        this.facility = response.data;
      });
    },
    /** 查询实有小区统计列表 */
    getDistrictList() {
      this.loading = true;
      this.queryParams.id = this.districtId
      listDistrict(this.queryParams).then(response => {
        this.districtList = response.rows;
        this.loading = false;
      });
    },
    /** 查询实有安防设施列表 */
    getList() {
      this.loading = true;
      var item = localStorage.getItem('districtId')
      this.queryParams.districtId = item
      listFacilities(this.queryParams).then(response => {
        this.facilitiesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        facilityType: null,
        districtId: null,
        installedDate: null,
        maintenanceDate: null,
        createDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(id) {
      if (this.selectedItems.includes(id)) {
        // 如果选中项中包含当前 id，则从 ids 中添加该 id
        if (!this.ids.includes(id)) {
          this.ids.push(id);
        }
      } else {
        // 如果选中项中不包含当前 id，则从 ids 中移除该 id
        const index = this.ids.indexOf(id);
        if (index !== -1) {
          this.ids.splice(index, 1);
        }
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实有安防设施";
    },
    handleUpdate(){
      this.reset();
      if (this.ids.length === 1) {
        const id = this.ids[0];
        /* this.$message.info("开始修改操作，id：" + id); */
        getFacilities(id).then(response => {
          this.form = response.data;
          console.log('response.data1',this.form);
          this.open = true;
          this.title = "修改实有车辆信息";
        });
      } else if (this.ids.length > 1) {
        this.$message.warning("每次只能修改一个车辆信息统计，请选择单个项目进行修改。");
      } else {
        this.$message.warning("请选择一个车辆信息统计项目进行修改。");
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.districtId = this.districtId
          if (this.form.id != null) {
            updateFacilities(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFacilities(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getDistrictList()
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (this.ids.length === 0) {
        this.$modal.msgError("请选择要删除的数据");
        return;
      }
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实有安防设施编号为"' + ids + '"的数据项？').then(function() {
        return delFacilities(ids);
      }).then(() => {
        this.getDistrictList()
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('community/facilities/export', {
        ...this.queryParams
      }, `facilities_${new Date().getTime()}.xlsx`)
    },
    // 小区下拉框查询
    getSelect(){
      getDistrictSelect().then(res=>{
        console.log(res)
        this.district=res.data
      })
    },
    // 查询小区 实有人口
    handlePopulationClick(id) {
      this.$router.push({
        path: '/community/population/' + id,
        name: 'Population',
        params: {
          id: id
        }
      })
    },
    // 查询小区 实有房屋
    handleEstateClick(id) {
      this.$router.push({
        path: '/community/estate/' + id,
        name: 'Estate',
        params: {
          id: id
        }
      })
    },

    // 查询小区 实有单位
    handleUnitClick(id) {
      this.$router.push({
        path: '/community/unit/' + id,
        name: 'Unit',
        params: {
          id: id
        }
      })
    },
    // 查询小区 实有车辆
    handleVehiclesClick(id) {
      this.$router.push({
        path: '/community/vehicles/' + id,
        name: 'Vehicles',
        params: {
          id: id
        }
      })
    },
    // 查询小区 实有设备
    handleFacilitiesClick(id) {
      this.$router.push({
        path: '/community/facilities/' + id,
        name: 'Facilities',
        params: {
          id: id
        }
      })
    },
    // 查询小区 实有力量
    handleCacheClick() {
      alert('待开发');
    },
  }
};
</script>
<style scoped>
.product-content1 {
  text-align: center; /* 图片和信息居中对齐 */
  margin-bottom: 20px; /* 底部间距 */
}

.product-content1 .fixed-image-size {
  display: block; /* 确保图片是块级元素 */
  margin: 0 auto 10px auto; /* 图片居中并留下一定的底部间距 */
  max-width: 100%; /* 图片最大宽度为容器宽度 */
  height: 500px; /* 高度自适应，保持比例 */
  width: 500px; /* 固定宽度，可以根据需要调整 */
}

#box {
  display: flex;
  justify-content: center; /* 居中显示 */
}

.unit-list {
  display: flex;
  flex-wrap: wrap;
  padding: 0;
  margin: 0;
  list-style: none;
}
.unit-item {
  width: calc(33.33% - 20px); /* 计算每个项目的宽度，减去外边距 */
  margin: 10px; /* 外边距 */
}

.product-unit {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 10px;
  border: 1px solid #ccc;
  width: 100%; /* 确保每个项目占据父容器的宽度 */
}

.product-unit .fixed-image-size1 {
  display: block; /* 确保图片是块级元素 */
  margin: 0 auto 10px auto; /* 图片居中并留下一定的底部间距 */
  max-width: 100%; /* 图片最大宽度为容器宽度 */
  height: 200px; /* 高度自适应，保持比例 */
  width: 200px; /* 固定宽度，可以根据需要调整 */
}

.info {
  flex: 1; /* 让信息部分占据剩余空间 */
}

.info h4,
.info p {
  margin: 0;
}

.additional-info1 {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 三列布局 */
  grid-gap: 10px; /* 设置网格之间的间距 */
}

.additional-info1 p {
  margin: 0;
}
</style>
